package com.study.shiro;

import org.apache.shiro.authc.*;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.apache.shiro.util.ByteSource;

public class ShiroRealm extends AuthenticatingRealm {

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("进入ShiroRealm:["+authenticationToken+"]");
//        强行转换为login传入的Token类型
//        里面保存了前台输入的用户名密码
        UsernamePasswordToken token= (UsernamePasswordToken) authenticationToken;
//        与数据库交互获取
//        认证的实体信息
        Object Username=null;
        ByteSource byteSource=null;
        String pwd=null;
        if("admin".equals(token.getUsername())){
            Username="admin";
            byteSource=ByteSource.Util.bytes(Username);
//        密码
            pwd="038bdaf98f2037b31f1e75b5b4c9b26e";

        }
        else if("user".equals(token.getUsername())){
            Username="user";
            byteSource=ByteSource.Util.bytes(Username);
//        密码
            pwd="098d2c478e9c11555ce2823231e02ec1";
        }
//        进行比较
        AuthenticationInfo info = new SimpleAuthenticationInfo(Username, pwd, byteSource, getName());
        return info;
    }

    public static void main(String[] args) {
        SimpleHash simple=new SimpleHash("MD5","123456","user",1024);
        System.out.println(simple);
    }
}
